import jenkins


class Jenkinsapi(object):
    def __init__(self, url, user, token):
        self.server_url = url
        self.user = user
        self.token = token
        self.conn = jenkins.Jenkins(self.server_url, username=self.user, password=self.token)

    def get_jobs(self):
        """
        获取所有的构建项目列表
        :return:
        """
        return self.conn.get_jobs()

    def get_job_info(self, job):
        """
        根据项目名获取构建项目
        :param job:
        :return:
        """
        return self.conn.get_job_info(job)

    def build_job(self, job, **kwargs):
        """
        开始构建项目
        :param job:
        :param kwargs:
        :return:
        """
        # dict1 = {"version":11} # 参数话构建
        # dict2 = {'Status': 'Rollback', 'BUILD_ID': '26'} # 回滚
        return self.conn.build_job(job, parameters=kwargs)

    def get_build_info(self, job, build_number):
        """
        通过构建编号获取构建项目的构建记录
        :param job:
        :param build_number:
        :return:
        """
        return self.conn.get_build_info(job, build_number)

    def get_job_config(self, job):
        '''
        获取xml文件
        '''
        res = self.conn.get_job_config(job)
        print(res)

    def create_job(self, name, config_xml):
        '''
        任务名字
        xml格式的字符串
        '''
        self.conn.create_job(name, config_xml)

    def update_job(self, name, config_xml):
        res = self.conn.reconfig_job(name, config_xml)
        print(res)


if __name__ == '__main__':
    server_url = 'http://10.111.111.11:8080/'
    # username = 'admin'
    # password = '111111'
    token = '11eff69df6367842a682b81fd7c9f87694'
    # # server = Jenkinsapi(server_url, username, password)
    # # 基于登陆密码连接jenkins
    # server = jenkins.Jenkins(
    #     url=server_url,
    #     username=username,
    #     password=password
    # )

    # 基于token连接jenkins
    server = jenkins.Jenkins(
        'http://10.111.111.11:8080/',
        username='admin',
        password=token,
        timeout=0.1
    )
    print(server.get_whoami())
